<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-date"></i> 表单</el-breadcrumb-item>
                <el-breadcrumb-item>兑换码添加</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="form-box mt">
            <el-form ref="form" label-position="left" :rules="rules" :model="form" label-width="80px">
                <el-form-item label="优惠券" prop="coupon_id">
                  <el-select v-model="form.coupon_id" placeholder="请选择">
                    <el-option
                      v-for="item in coupon_list"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="标题" prop="title">
                  <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="数量" prop="num">
                  <el-input v-model.number="form.num"></el-input>
                </el-form-item>
                <el-form-item label="活动时间">
                  <el-col :span="11">
                      <el-form-item prop="start_time">
                        <el-date-picker type="date" placeholder="选择开始日期" v-model="form.start_time" style="width: 100%;"></el-date-picker>
                      </el-form-item>
                  </el-col>
                  <el-col class="line" :span="2">-</el-col>
                  <el-col :span="11">
                    <el-form-item prop="end_time">
                      <el-date-picker type="date" placeholder="选择结束日期" v-model="form.end_time" style="width: 100%;"></el-date-picker>
                    </el-form-item>
                  </el-col>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit('form')">提交</el-button>
                    <el-button @click="navTop">取消</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import bus from '../../../bus.js';
export default {
  name: 'v-form',
  data: function() {
    return {
      coupon_list: null,
      form: {
        coupon_id: undefined,
        num: undefined,
        start_time: undefined,
        end_time: undefined,
        title: undefined
      },
      rules: {
        coupon_id: [
            { required: true, message: '此项必填' }
        ],
        title: [
            { required: true, message: '此项必填' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        num: [
          { required: true, message: '此项必填' },
          { type: 'number', message: '必须为数字值，且在1-4之间'}
        ],
        start_time: [
          { required: true, message: '日期选择为必填' }
        ],
        end_time: [
          { required: true, message: '日期选择为必填' }
        ]
      }
    }
  },
  methods: {
    onSubmit(formName) {
      //  进行规则验证
      if(this.form.num > 4 || this.form.num <= 0) {
        this.$message({
          message: '数量在1-4之间',
          type: 'warning'
        });
        return;
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          APP.ajax('post@redeemRecord/add', this.form, res => {
            if (res.data.status === 200) {
                this.$message.success('添加成功！');
                this.form = null;
            } else {
              this.$message({
                message: res.data.msg,
                type: 'warning'
              });
            }
          })
        } else {
          return false;
        }
      });
    },
    navTop() {
      bus.$emit('ViewShow', '取消');
    },
    getData() {
      // 获取优惠券列表数据
      APP.ajax('coupon/list', {}, res => {
        this.coupon_list = res.data.data.data;
      });
    }
  },
  mounted () {
    // 获取优惠券数据进行选择
    this.getData();
  }
}
</script>
<style scoped>
.avatar{
    float: left;
    width: 100%;
    height: 100%;
}
</style>

